package com.lantosec.hmrp.modules.revisit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.revisit.entity.HisPatientInfo;
import com.lantosec.hmrp.modules.revisit.entity.RevisitConfig;
import com.lantosec.hmrp.modules.revisit.entity.RevisitNote;
import com.lantosec.hmrp.modules.revisit.entity.RevisitTask;
import com.lantosec.hmrp.modules.revisit.mapper.RevisitNoteMapper;
import com.lantosec.hmrp.modules.revisit.service.IRevisitNoteService;
import com.lantosec.hmrp.modules.revisit.service.IRevisitTaskService;
import com.lantosec.hmrp.modules.revisit.vo.RevisitNoteVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 回访及复核记录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-05-06
 */
@Service
@Primary
@Log4j2
public class RevisitNoteServiceImpl extends ServiceImpl<RevisitNoteMapper, RevisitNote> implements IRevisitNoteService {

    @Autowired
    private IRevisitTaskService iRevisitTaskService;

    @Autowired
    private RevisitConfigServiceImpl revisitConfigService;

    @Override
    public void startRevisit(RevisitNote entity) {
        Integer revisitTaskId = entity.getRevisitTaskId();
        //查询回访任务信息
        RevisitTask revisitTask = iRevisitTaskService.getById(revisitTaskId);
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        entity.setRevisitUserId(userId);
        entity.setRevisitUser(userName);
        entity.setRevisitTime(DateUtils.nowDate());
        //根据部门id查询配置表中的最短通话时间
        String deptId = revisitTask.getDeptId();
        RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(),deptId));
        Integer callDurationStd = revisitConfig.getCallDurationStd();
        //如果配置表中的最短通话时间大于回访通话时间，则此次回访无效
        if(callDurationStd > entity.getCallDuration()){
            entity.setInvalid(0);
        }else {
            entity.setInvalid(1);
        }
        //新增回访记录
        this.save(entity);

        // 查询该用户的岗位
        String position = baseMapper.getPositionByUserId(userId);
        //如果当前用户是主管医生
        if(position.equals("医生")){
            revisitTask.setRevisitTaskId(revisitTaskId);
            //判断是否是超时回访
            Date outTime = revisitTask.getOutTime();
            Date addDate = new Date();
            try {
                //计算出院时间加上回访期限
                addDate = DateUtils.plusDay(revisitConfig.getOverDays(),outTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date newDate = DateUtils.nowDate();
            //如果当前时间大于出院时间+回访期限
            if(addDate.compareTo(newDate) == -1){
                revisitTask.setDoctorState(2);
            }else{
                revisitTask.setDoctorState(1);
            }
            iRevisitTaskService.updateById(revisitTask);
        }
        if(position.equals("护士")){
            revisitTask.setRevisitTaskId(revisitTaskId);
            //判断是否是超时回访
            Date outTime = revisitTask.getOutTime();
            Date addDate = new Date();
            try {
                //计算出院时间加上回访期限
                addDate = DateUtils.plusDay(revisitConfig.getOverDays(),outTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date newDate = DateUtils.nowDate();
            //如果当前时间大于回访时间期限
            if(addDate.compareTo(newDate) == -1){
                revisitTask.setNurseState(2);
            }else{
                revisitTask.setNurseState(1);
            }
            iRevisitTaskService.updateById(revisitTask);
        }
    }

    @Override
    public void review(RevisitNote entity) {
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        entity.setReviewUserId(userId);
        entity.setReviewUser(userName);
        entity.setReviewTime(DateUtils.nowDate());
        this.updateById(entity);
    }

    @Override
    public List<RevisitNoteVO> getAll(PageVO<RevisitNoteVO> page, String date) {
        List<RevisitNoteVO> revisitNoteVOList = baseMapper.getAll(page,date);
        if(null != revisitNoteVOList && revisitNoteVOList.size() >0){
            revisitNoteVOList.forEach(parm->{
                //查询患者信息
                Integer revisitTaskId = parm.getRevisitTaskId();
                RevisitTask revisitTask = iRevisitTaskService.getById(revisitTaskId);
                HisPatientInfo hisPatientInfo = baseMapper.getHisPatientInfo(revisitTask.getPatientId());
                parm.setHisPatientInfo(hisPatientInfo);
            });
        }
        page.setRecords(revisitNoteVOList);
        return revisitNoteVOList;
    }

    @Override
    public List<HisPatientInfo> getHisPatientInfo(String date) {
        List<HisPatientInfo> hisPatientInfoList = baseMapper.getHisPatientInfoParam(date);
        return hisPatientInfoList;
    }
}
